package com.cea.papyrus.diagram.sequence.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.TimeObservation;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete {@link TimeObservation}.
 * 
 */
public class NodeTimeObservationHelper extends NodeHelper {

	/**
     * {@inheritDoc}
     */
    public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location,
            Dimension size) {
        Debug.debug(this, "Call NodeTimeObservationHelper.createGraphElement()", channel); //$NON-NLS-1$
	            
		// set default color for created element
		GraphNode graphNode=super.createGraphElement(umlChild, null, location, size);
		
		if(parent != null) {
			parent.getContained().add(graphNode);
		}
		return graphNode;
    }

    /**
     * {@inheritDoc}
     */
    public boolean canCreateGraphElement(GraphElement parent) {
        Debug.debug(this, "Call NodeTimeObservationHelper.canCreateGraphElement()", channel); //$NON-NLS-1$
	        
		if (parent.getSemanticModel() != null) {
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt != null){
				return true;
			}
		}
		return false;
    }

    // /////////////////////////////////////////////////////////////////////////
    // UML Management
    // /////////////////////////////////////////////////////////////////////////

    /**
     * {@inheritDoc}
     */
    public org.eclipse.uml2.uml.Element createUMLElement(String name,
            org.eclipse.uml2.uml.Element umlParent) {
        Debug.debug(this, "Call NodeTimeObservationHelper.createUMLElement()", channel); //$NON-NLS-1$
        org.eclipse.uml2.uml.TimeObservation timeObservation = UMLFactory.eINSTANCE.createTimeObservation();
        timeObservation.setName(name);
        addUMLElement(timeObservation, umlParent);
        return timeObservation;
    }

    /**
     * {@inheritDoc}
     */
    public boolean canCreateUMLElement(Element umlParent) {
        Debug.debug(this, "Call NodeTimeObservationHelper.canCreateUMLElement()", channel); //$NON-NLS-1$
        if (umlParent instanceof Package) {
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    /**
     * {@inheritDoc}
     */
    public boolean addUMLElement(Element element, Element umlParent) {
        Debug.debug(this, "Call NodeTimeObservationHelper.addUMLElement()", channel); //$NON-NLS-1$
        
        umlParent.getNearestPackage().getPackagedElements().add((TimeObservation) element);
        
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public boolean removeUMLElement(Element element, Element umlParent) {
        Debug.debug(this, "Call NodeTimeObservationHelper.removeUMLElement()", channel); //$NON-NLS-1$
        if (umlParent instanceof Package) {
            return ((Package) umlParent).getPackagedElements().remove(element);
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public String getBaseString() {
        return "TimeObservation_"; //$NON-NLS-1$
    }

    /**
     * {@inheritDoc}
     */
    public void setGraphElementPreferences(GraphElement graphElement) {
        Debug.debug(this, "Call NodeTimeObservationHelper.setGraphElementPreferences()", channel); //$NON-NLS-1$
    }
}
